package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * @Classname BackfillInstallAddressAttrHandler
 * @Description 装机地址弹出框选择地址确认后，需要回填  装机地址、标准地址ID、楼宇编码、楼宇名称
 * @Author zhang.song
 * @Date 2021-03-03 11:03
 */
public class BackfillInstallAddressAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    IQuerySceneInstService querySceneInstService;
    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        String goodsId = MapUtils.getString(extParams,"goodsNodeId");
        String nodeCode = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "nodeCode");
//        String province = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
//        if(goodsId==null){
//            goodsId = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "goodsId");
//        }
        List<String> goodsNodeId = ProdCateUtil.getDoubleLineCateId();
        // 互联网类产品,装机地址属性在订单归集后的环节不允许再编辑
        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        // app甩单补录、资源核查补录情况，可以编辑
        String serviceOfferId = data.getServiceOfferId();
        String renderOrderType = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "renderOrderType");
        if (!KiteStringUtils.isEmpty(nodeCode) && !KiteStringUtils.isEqual(renderOrderType, "orderRejection")) {
            if(Objects.nonNull(attr)){
                // 五大双线追单重提页面可编辑范围优化
                // 批量受理单个修改页面 装机地址可以编辑
                if (KiteStringUtils.equalsAny(data.getServiceOfferId(), "1", "1012") && "OrderResubmit".equals(nodeCode)
                    && goodsNodeId.contains(goodsId) || KiteStringUtils.equals(data.getServiceOfferId(), "1014")
                    || CuccPosServiceUtils.isBatchAccept(data.getSceneInstId())) {
                    attr.setIsEdit(KeyConsts.IFTRUE_T);
                } else {
                    attr.setIsEdit(KeyConsts.IFTRUE_F);
                }
                String provinceStr = DcPublicCache.getPCode("2021092801","PROVINCE_TYPE");
                if (KiteStringUtils.equalsAny(attr.getFieldName(),"2_0000211","2_0000117") && KiteStringUtils.isNotEmpty(provinceStr)) {
                    KiteAttr addressAttr = null;
                    if (KiteStringUtils.equals(attr.getFieldName(),"2_0000211")) {
                        addressAttr = inst.getAttr("2_0000209");
                    } else if (KiteStringUtils.equals(attr.getFieldName(),"2_0000117")) {
                        addressAttr = inst.getAttr("2_0000105");
                    }
                    if (KiteStringUtils.isNotEmpty(addressAttr.getValue())) {
                        String[] valueArr = addressAttr.getValue().split(SymbolConsts.COMMA);
                        //签订、追单、重提、质检环节支持修改该业务属性
                        boolean flag = KiteStringUtils.equalsAny(nodeCode, "OrderSigned", "OrderResubmit", "OrderAudit") ||
                            KiteStringUtils.equalsAny(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW, "1", LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK);
                        if (!flag && valueArr.length > 0 && KiteStringUtils.equalsAny(valueArr[0], provinceStr.split(SymbolConsts.COMMA))) {
                            attr.setIsEdit("SF");

                        }
                    }
                }
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
            }
//            // 接入端口类型
//            KiteAttr accessPortTypeAttr = inst.getAttr("2_0000064");
//            if (Objects.nonNull(accessPortTypeAttr)) {
//                accessPortTypeAttr.setIsEdit(KeyConsts.IFTRUE_F);
//                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), accessPortTypeAttr);
//            }

//            // Z端装机地址
//            KiteAttr zInstallAddressAttr = inst.getAttr("2_0000105");
//            if (Objects.nonNull(zInstallAddressAttr)) {
//                zInstallAddressAttr.setIsEdit(KeyConsts.IFTRUE_F);
//                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), zInstallAddressAttr);
//            }
//            // 接口类型（A端）
//            KiteAttr accessPortATypeAttr = inst.getAttr("2_0000008");
//            if (Objects.nonNull(accessPortATypeAttr)) {
//                //上海订购重提 sdh和以太网可编辑
//                if (KiteStringUtils.equals(data.getServiceOfferId(),"1") && "OrderResubmit".equals(nodeCode) && "31".equals(province) && goodsNodeId.contains(goodsId)) {
//                        accessPortATypeAttr.setIsEdit(KeyConsts.IFTRUE_T);
//                }else{
//                    accessPortATypeAttr.setIsEdit(KeyConsts.IFTRUE_F);
//                }
//                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), accessPortATypeAttr);
//            }
//            // 接口类型（Z端）
//            KiteAttr accessPortZTypeAttr = inst.getAttr("2_0000010");
//            if (Objects.nonNull(accessPortZTypeAttr)) {
//                //上海订购重提 sdh和以太网可编辑
//                if (KiteStringUtils.equals(data.getServiceOfferId(),"1") && "OrderResubmit".equals(nodeCode) && "31".equals(province) && goodsNodeId.contains(goodsId)) {
//                    accessPortZTypeAttr.setIsEdit(KeyConsts.IFTRUE_T);
//                }else{
//                    accessPortZTypeAttr.setIsEdit(KeyConsts.IFTRUE_F);
//                }
//                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), accessPortZTypeAttr);
//            }
       }
        String cateId =  CuccPosServiceUtils.getCateId(data.getSceneInstId(),data.getCustId());
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(data.getSceneInstId());
        String provinceCode = sceneInst.getProvinceNbr();
        if (LocalKeyConsts.PROVINCE_ZHEJIANG.equals(provinceCode) && "2004029".equals(cateId) && !KiteStringUtils.equalsAny(serviceOfferId,"1","2826","1014")){
            KiteAttr standardAddressAttr = inst.getAttr("2_0000211");
            standardAddressAttr.setPageUrl("AttrInstallationCasAddress");
            operateSceneInstService.setAttr(data.getSceneInstId(),inst.getInstType(),inst.getInstId(),standardAddressAttr);
            String fieldNameS = DcPublicCache.getCodea("2022012301","rejudge_prod_e");
            List fieldNameList = Arrays.asList(fieldNameS.split(","));
            if (KiteListUtils.isNotEmpty(fieldNameList)){
                fieldNameList.forEach(fieldName->{
                    KiteAttr kiteAttr = querySceneInstService.getInstAttr(data.getCustId(),inst.getInstType(),inst.getInstId(),KiteStringUtils.valueOf(fieldName));
                    if (Objects.nonNull(kiteAttr)){
                        kiteAttr.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                        operateSceneInstService.setAttr(data.getSceneInstId(),inst.getInstType(),inst.getInstId(),kiteAttr);
                    }
                });
            }

        }
        super.onShow(attr, inst, data);
        this.setIsEdit(attr, inst, data);
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        this.setIsEdit(attr, inst, data);
    }

    @Override
    public void def(KiteAttr attr, KiteInst inst, AttrData data) {

        this.setIsEdit(attr, inst, data);
    }

    public void setIsEdit(KiteAttr attr, KiteInst inst, AttrData data) {
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        DcPublic mapAidConfig = DcPublicCache.getByPcode("2202241001", "MAP_AID_QRY_BTN", inst.getSpecId()).stream().findFirst().orElse(null);
        if (Objects.nonNull(mapAidConfig)) {
            String provConfig = mapAidConfig.getCodea();
            if ((provConfig.contains(provinceCode) || KiteStringUtils.equals(provConfig, "-1"))) {

                String typeFlag = querySceneInstService.getExtParameterValue(data.getSceneInstId(), attr.getGroupId() + "_" + attr.getFieldName());
                if (KiteStringUtils.equals(typeFlag, "3")) {
                    return;
                }
                attr.setIsEdit("SF");
            }
        }
    }


    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.set(attr, inst, value, data);
        // 增加校验
//        String prevValue = attr.getPreValue();
//        if (KiteStringUtils.isNotEmpty(prevValue)) {
//            return;
//        }
//        if (!value.startsWith(prevValue)) {
//            ErrorConsts.BUSI_CHECK_ERROR.throwOut("不支持对标准地址的原始内容进行编辑,仅支持对标准地址内容进行追加操作!");
//        }
    }
}
